/**
 * 
 */
package org.codesketch.controller.service.impl;

import java.util.concurrent.Callable;

import org.codesketch.controller.common.Factory;
import org.codesketch.controller.domain.Status;
import org.codesketch.controller.domain.Test;
import org.codesketch.controller.exception.TestExecutionException;
import org.codesketch.controller.exception.UnprocessabeEntityException;
import org.codesketch.controller.schedule.TestRunner;

class Executor implements Callable<Test>, Factory<TestRunner> {
	private Test test;
	
	public Executor(Test test) {
		this.test = test;
	}
	
	@Override
	public Test call() throws Exception {
		try {
			TestRunner runner = create();
			runner.run(test);
			test.setStatus(Status.RUNNING);
			test.setStartTime(System.currentTimeMillis());
		} catch(TestExecutionException e) {
			test.setStatus(Status.DEPLOYING_ERROR);
		} catch (UnprocessabeEntityException e) {
			test.setStatus(Status.CONFIGURATION_ERROR);
		} catch (Exception e) {
			test.setStatus(Status.GENERIC_ERROR);
		}
		return test;
	}

	/* (non-Javadoc)
	 * @see org.codesketch.controller.common.Factory#create()
	 */
	@Override
	public TestRunner create() {
		return new TestRunner();
	}
	
}